home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dviselect / font_subr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-16  |  4.7 KB  |  241 lines

  1. /*
  2.  * Copyright (c) 1987 University of Maryland Department of Computer Science.
  3.  * All rights reserved.  Permission to copy for any purpose is hereby granted
  4.  * so long as this copyright notice remains intact.
  5.  */
  6.  
  7. #ifndef lint
  8. static char rcsid[] = "$Header: font_subr.c,v 1.1 88/02/11 17:08:49 jim Exp $";
  9. #endif
  10.  
  11. /*
  12.  * Subroutines common to all fonts.
  13.  */
  14.  
  15. #include "font.h"
  16.  
  17. static struct glyph *freeglyphs;
  18.  
  19. char    *malloc();
  20. extern    errno;
  21.  
  22. /*
  23.  * Set up the font structures to note that a font has glyphs in
  24.  * the half-open interval [low, high).
  25.  *
  26.  * SHOULD I ALLOW ADDITIONS TO THE RANGE VIA SUBSEQUENT CALLS TO
  27.  * FontHasGlyphs?
  28.  */
  29. FontHasGlyphs(f, low, high)
  30.     register struct font *f;
  31.     register int low, high;
  32. {
  33.     register struct glyph **gp;
  34.  
  35.     /* record bounds */
  36.     f->f_lowch = low;
  37.     f->f_highch = high;
  38.  
  39.     /*
  40.      * Allocate space for all the glyph pointers, and set
  41.      * them all to NULL.
  42.      */
  43.     if (low >= high)    /* no glyphs */
  44.         gp = NULL;
  45.     else {
  46.         gp = (struct glyph **) malloc((unsigned) (high - low) *
  47.             sizeof (*gp));
  48.         if (gp == NULL)
  49.             return (-1);
  50.     }
  51.     f->f_glybase = gp;
  52.     f->f_gly = gp - low;
  53.     while (++low <= high) {
  54.         *gp = NULL;
  55.         gp++;
  56.         }
  57.     return (0);
  58. }
  59.  
  60. /*
  61.  * AllocGlyph allocates a new glyph.  ReleaseGlyph puts one onto the free
  62.  * list.  We maintain a local list of free glyphs.
  63.  */
  64. #define ReleaseGlyph(g)    \
  65.     ((g)->g_un.g_next = freeglyphs, freeglyphs = (g))
  66.  
  67. static struct glyph *
  68. AllocGlyph(n)
  69.     int n;
  70. {
  71.     register struct glyph *g;
  72.     register int i;
  73.  
  74.     if ((g = freeglyphs) == NULL) {
  75.         g = (struct glyph *) malloc((unsigned) (128 * sizeof (*g)));
  76.         if (g == NULL)
  77.             error(1, errno, "out of glyph memory");
  78.         g += (i = 128);
  79.         while (--i >= 0) {
  80.             g--;
  81.             ReleaseGlyph(g);
  82.         }
  83.     }
  84.     freeglyphs = g->g_un.g_next;
  85.     g->g_flags = 0;
  86.     g->g_raster = NULL;
  87.     g->g_index = n;
  88.     return (g);
  89. }
  90.  
  91. /*
  92.  * Free one glyph.
  93.  */
  94. void
  95. FreeGlyph(f, n)
  96.     struct font *f;
  97.     register int n;
  98. {
  99.     register struct glyph *g;
  100.  
  101.     if (n < f->f_lowch || n >= f->f_highch)
  102.         return;
  103. #ifdef notdef
  104.     (*f->f_ops->fo_freegly)(f, n, n);
  105. #endif
  106.     if ((g = f->f_gly[n]) == NULL)
  107.         return;
  108.     if (g->g_raster != NULL)
  109.         free(g->g_raster);
  110.     ReleaseGlyph(g);
  111. }
  112.  
  113. /*
  114.  * Free a font.
  115.  */
  116. void
  117. FreeFont(f)
  118.     register struct font *f;
  119. {
  120.     register struct glyph *g;
  121.     register int i;
  122.  
  123. #ifdef notdef
  124.     (*f->f_ops->fo_freegly)(f, f->f_lowch, f->f_highch);
  125. #endif
  126.     for (i = f->f_lowch; i < f->f_highch; i++) {
  127.         if ((g = f->f_gly[i]) == NULL)
  128.             continue;
  129.         if (g->g_raster != NULL)
  130.             free(g->g_raster);
  131.         ReleaseGlyph(g);
  132.     }
  133.     if (f->f_glybase != NULL)
  134.         free((char *) f->f_glybase);
  135.     (*f->f_ops->fo_freefont)(f);
  136.     free(f->f_path);
  137.     free(f->f_font);
  138.     free((char *) f);
  139. }
  140.  
  141. /*
  142.  * Get glyph `c' in font `f'.  We pull in a few adjacent glyphs here
  143.  * under the (perhaps naive) assumption that things will go faster
  144.  * that way.
  145.  *
  146.  * TODO:
  147.  *    try different adjacency values
  148.  *    make adjacency a font attribute? (or an op)
  149.  */
  150. #define    ADJ    8        /* must be a power of 2 */
  151. #define    GET_ADJ(c, l, h) ((h) = ADJ + ((l) = (c) & ~(ADJ - 1)))
  152.  
  153. struct glyph *
  154. GetGlyph(f, c)
  155.     register struct font *f;
  156.     int c;
  157. {
  158.     register int i, h, l;
  159.  
  160.     GET_ADJ(c, l, h);
  161.     if (l < f->f_lowch)
  162.         l = f->f_lowch;
  163.     if (h > f->f_highch)
  164.         h = f->f_highch;
  165.     if (l >= h)
  166.         return (NULL);
  167.     for (i = l; i < h; i++)
  168.         if (f->f_gly[i] == NULL)
  169.             f->f_gly[i] = AllocGlyph(i);
  170.  
  171.     if ((*f->f_ops->fo_getgly)(f, l, h)) {
  172.         /*
  173.          * I do not know what to do about this just yet, so:
  174.          */
  175.         panic("getgly fails and I am confused ... help!");
  176.     }
  177.  
  178.     /*
  179.      * Apply the appropriate scale factor to the TFM widths.
  180.      * This makes them come out in scaled points, instead of FIXes.
  181.      */
  182.     ScaleGlyphs(f, l, h);    /* ??? */
  183.  
  184.     return (f->f_gly[c]);
  185. }
  186.  
  187. /*
  188.  * Get the raster for glyph g in font f at rotation r.
  189.  */
  190. char *
  191. GetRaster(g, f, r)
  192.     register struct glyph *g;
  193.     register struct font *f;
  194.     int r;
  195. {
  196.     int l, h;
  197.  
  198.     /* abort if caller did not ask for rasters in advance */
  199.     if ((f->f_flags & FF_RASTERS) == 0)
  200.         panic("GetRaster(%s)", f->f_path);
  201.  
  202.     /*
  203.      * If there is no raster, get one.  Blank characters,
  204.      * however, never have rasters.
  205.      */
  206.     if (g->g_raster == NULL) {
  207.         if (!HASRASTER(g))
  208.             return (NULL);
  209.         /*
  210.          * THE FOLLOWING DEPENDS ON THE ADJACENCY MATCHING THAT IN
  211.          * GetGlyph() ABOVE.
  212.          */
  213.         GET_ADJ(g->g_index, l, h);
  214.         if (l < f->f_lowch)
  215.             l = f->f_lowch;
  216.         if (h > f->f_highch)
  217.             h = f->f_highch;
  218.         if ((*f->f_ops->fo_rasterise)(f, l, h))
  219.             error(1, 0, "rasterise fails (out of memory?)");
  220.     }
  221.     if (g->g_rotation != r)
  222.         SetRotation(g, r);
  223.     return (g->g_raster);
  224. }
  225.  
  226. /*
  227.  * Return a TeX-style font name, including scale factor.
  228.  * SHOULD I BOTHER WITH \magstep STYLE NAMES?
  229.  */
  230. char *
  231. Font_TeXName(f)
  232.     register struct font *f;
  233. {
  234.     static char result[200];
  235.  
  236.     if (f->f_scaled == 1000)
  237.         return (f->f_font);
  238.     sprintf(result, "%s scaled %d", f->f_font, f->f_scaled);
  239.     return (result);
  240. }
  241.